package com.generate.library;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import jregex.Pattern;

import com.generate.control.IReplace;
import com.generate.control.JComboboxReplace;
import com.generate.control.JTextFieldReplace;

public class ImpexUtils {
	private ImpexUtils() {
	}

	public static Map<String, String> mapAttributes;
	public static Map<String, String> mapValues = new HashMap<String, String>();
	public static Map<String, IReplace> mapComponent = new HashMap<String, IReplace>();
	public static String[] ATTRIBUTES = new String[] {
			"ELECTRONICSCOLORVARIANTPRODUCTTYPE",
			"@ELECTRONICS_COLOR_VARIANT_PRODUCT_TYPE@", "SUPER_CATEGORIES",
			"@SUPER_CATEGORIES@", "SALESTATUS", "@SALE_STATUS@",
			"VARIANTNUMBER", "@VARIANT_NUMBER@", "IMAGEDETAILNUMBER",
			"@IMAGE_DETAIL_NUMBER@", "BASEPRODUCTCODE", "@BASE_PRODUCT_CODE@",
			"BASEPRODUCTNAME", "@BASE_PRODUCT_NAME@", "BASEPRODUCTPRICES",
			"@BASE_PRODUCT_PRICES@",

			"VARIANT1CODE", "@VARIANT_1_CODE@", "VARIANT1NAME",
			"@VARIANT_1_NAME@", "VARIANT1PRICES", "@VARIANT_1_PRICES@",
			"VARIANT1WASPRICES", "@VARIANT_1_WAS_PRICES@", "VARIANT1IMAGENAME",
			"@VARIANT_1_IMAGE_NAME@", "VARIANT1PROMOTIONTEXT",
			"@VARIANT_1_PROMOTION_TEXT@", "VARIANT1SUMMARY",
			"@VARIANT_1_SUMMARY@", "VARIANT1SUMMARYRICHTEXT",
			"@VARIANT_1_SUMMARY_RICH_TEXT@",

			"VARIANT2CODE", "@VARIANT_2_CODE@", "VARIANT2NAME",
			"@VARIANT_2_NAME@", "VARIANT2PRICES", "@VARIANT_2_PRICES@",
			"VARIANT2WASPRICES", "@VARIANT_2_WAS_PRICES@", "VARIANT2IMAGENAME",
			"@VARIANT_2_IMAGE_NAME@", "VARIANT2PROMOTIONTEXT",
			"@VARIANT_2_PROMOTION_TEXT@", "VARIANT2SUMMARY",
			"@VARIANT_2_SUMMARY@", "VARIANT2SUMMARYRICHTEXT",
			"@VARIANT_2_SUMMARY_RICH_TEXT@",

			"VARIANT3CODE", "@VARIANT_3_CODE@", "VARIANT3NAME",
			"@VARIANT_3_NAME@", "VARIANT3PRICES", "@VARIANT_3_PRICES@",
			"VARIANT3WASPRICES", "@VARIANT_3_WAS_PRICES@", "VARIANT3IMAGENAME",
			"@VARIANT_3_IMAGE_NAME@", "VARIANT3PROMOTIONTEXT",
			"@VARIANT_3_PROMOTION_TEXT@", "VARIANT3SUMMARY",
			"@VARIANT_3_SUMMARY@", "VARIANT3SUMMARYRICHTEXT",
			"@VARIANT_3_SUMMARY_RICH_TEXT@",

			"VARIANT4CODE", "@VARIANT_4_CODE@", "VARIANT4NAME",
			"@VARIANT_4_NAME@", "VARIANT4PRICES", "@VARIANT_4_PRICES@",
			"VARIANT4WASPRICES", "@VARIANT_4_WAS_PRICES@", "VARIANT4IMAGENAME",
			"@VARIANT_4_IMAGE_NAME@", "VARIANT4PROMOTIONTEXT",
			"@VARIANT_4_PROMOTION_TEXT@", "VARIANT4SUMMARY",
			"@VARIANT_4_SUMMARY@", "VARIANT4SUMMARYRICHTEXT",
			"@VARIANT_4_SUMMARY_RICH_TEXT@",

			"VARIANT5CODE", "@VARIANT_5_CODE@", "VARIANT5NAME",
			"@VARIANT_5_NAME@", "VARIANT5PRICES", "@VARIANT_5_PRICES@",
			"VARIANT5WASPRICES", "@VARIANT_5_WAS_PRICES@", "VARIANT5IMAGENAME",
			"@VARIANT_5_IMAGE_NAME@", "VARIANT5PROMOTIONTEXT",
			"@VARIANT_5_PROMOTION_TEXT@", "VARIANT5SUMMARY",
			"@VARIANT_5_SUMMARY@", "VARIANT5SUMMARYRICHTEXT",
			"@VARIANT_5_SUMMARY_RICH_TEXT@",

			"VARIANT6CODE", "@VARIANT_6_CODE@", "VARIANT6NAME",
			"@VARIANT_6_NAME@", "VARIANT6PRICES", "@VARIANT_6_PRICES@",
			"VARIANT6WASPRICES", "@VARIANT_6_WAS_PRICES@", "VARIANT6IMAGENAME",
			"@VARIANT_6_IMAGE_NAME@", "VARIANT6PROMOTIONTEXT",
			"@VARIANT_6_PROMOTION_TEXT@", "VARIANT6SUMMARY",
			"@VARIANT_6_SUMMARY@", "VARIANT6SUMMARYRICHTEXT",
			"@VARIANT_6_SUMMARY_RICH_TEXT@",

			"VARIANT7CODE", "@VARIANT_7_CODE@", "VARIANT7NAME",
			"@VARIANT_7_NAME@", "VARIANT7PRICES", "@VARIANT_7_PRICES@",
			"VARIANT7WASPRICES", "@VARIANT_7_WAS_PRICES@", "VARIANT7IMAGENAME",
			"@VARIANT_7_IMAGE_NAME@", "VARIANT7PROMOTIONTEXT",
			"@VARIANT_7_PROMOTION_TEXT@", "VARIANT7SUMMARY",
			"@VARIANT_7_SUMMARY@", "VARIANT7SUMMARYRICHTEXT",
			"@VARIANT_7_SUMMARY_RICH_TEXT@",

			"VARIANT8CODE", "@VARIANT_8_CODE@", "VARIANT8NAME",
			"@VARIANT_8_NAME@", "VARIANT8PRICES", "@VARIANT_8_PRICES@",
			"VARIANT8WASPRICES", "@VARIANT_8_WAS_PRICES@", "VARIANT8IMAGENAME",
			"@VARIANT_8_IMAGE_NAME@", "VARIANT8PROMOTIONTEXT",
			"@VARIANT_8_PROMOTION_TEXT@", "VARIANT8SUMMARY",
			"@VARIANT_8_SUMMARY@", "VARIANT8SUMMARYRICHTEXT",
			"@VARIANT_8_SUMMARY_RICH_TEXT@" };

	public static void init() {
		mapAttributes = new HashMap<String, String>();
		for (int i = 0; i < ATTRIBUTES.length; i += 2) {
			mapAttributes.put(ATTRIBUTES[i], ATTRIBUTES[i + 1]);
		}

		for (IReplace replace : IReplace.lstReplaceComponent) {
			mapComponent.put(replace.getFindText(), replace);
		}
	}

	public static void loadFolder(File folder) {
		mapValues.clear();
		for (File file : GenerateUtils.listFilesForFolder(folder)) {
			if (file.isFile()) {
				try {
					readString(GenerateUtils.readFile(file.getPath()));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private static void readString(String fileStr) {
		if (mapAttributes == null) {
			init();
		}
		Pattern pattern = new Pattern(
				"\\$([\\w\\d_-]+)\\s*=[\\t ]*(.*?)\\s*\\r?\\n", Pattern.DOTALL
						+ Pattern.IGNORE_CASE);
		jregex.Matcher matcher = pattern.matcher(fileStr);
		while (matcher.find()) {
			if (mapAttributes.containsKey(matcher.group(1).toUpperCase())) {
				if (mapComponent.get(mapAttributes.get(matcher.group(1)
						.toUpperCase())) instanceof JTextFieldReplace) {
					((JTextFieldReplace) mapComponent.get(mapAttributes
							.get(matcher.group(1).toUpperCase())))
							.setText(matcher.group(2));
				} else if (mapComponent.get(mapAttributes.get(matcher.group(1)
						.toUpperCase())) instanceof JComboboxReplace) {
					((JComboboxReplace) mapComponent.get(mapAttributes
							.get(matcher.group(1).toUpperCase())))
							.setSelectedItem(matcher.group(2));
				}
				mapValues.put(
						mapAttributes.get(matcher.group(1).toUpperCase()),
						matcher.group(2));
			}
		}
	}
}
